package com.ahsjdy.st.stservice.service.impl;

import com.ahsjdy.st.stdao.entity.Customer;
import com.ahsjdy.st.stdao.entity.User;
import com.ahsjdy.st.stdao.mapper.CustomerMapper;
import com.ahsjdy.st.stdao.mapper.UserMapper;
import com.ahsjdy.st.stservice.service.UserService;
import com.ahsjdy.stcommon.dto.ResponseDTO;
import com.ahsjdy.stcommon.dto.WXLoginDTO;
import com.ahsjdy.stcommon.exception.BizException;
import com.ahsjdy.stcommon.util.ApiConnector;
import com.ahsjdy.stcommon.util.WXUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description TODO
 * @Author xuetao wang
 * @Date 2019/12/29
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Value("${wx.appid}")
    protected String appId;

    @Value("${wx.secret}")
    protected String secret;

    @Value("${wx.grant.type}")
    protected String grantType;

    @Value("${wx.login.url}")
    protected String loginUrl;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private UserMapper userMapper;



    @Override
    public String wxLogin(String code) {

        StringBuilder sx = new StringBuilder();
        sx.append(loginUrl).append("?")
                .append("appid=").append(appId).append("&")
                .append("secret=").append(secret).append("&")
                .append("js_code=").append(code).append("&")
                .append("grant_type=").append(grantType);

        String result = ApiConnector.get(sx.toString(), null);
        logger.info("微信认证返回信息:{}", result);

        if (StringUtils.isBlank(result)) {
            throw new BizException("微信登录返回空");
        }

        WXLoginDTO wxLoginDTO = JSON.parseObject(result, WXLoginDTO.class);
        if (!StringUtils.isBlank(wxLoginDTO.getErrcode())) {
            throw new BizException("微信登录认证失败");
        }

        //认证成功 保存到数据
        logger.info("微信登录openid={}", wxLoginDTO.getOpenId());

//        Customer customer = customerMapper.getByOpenId(wxLoginDTO.getOpenId());
//        //新增用户
//        if (null == customer) {
//            customer = new Customer();
//            customer.setCreateTime(new Date());
//            customer.setOpenId(wxLoginDTO.getOpenId());
//            int rows = customerMapper.insert(customer);
//            if (1 != rows) {
//                throw new BizException("新增用户失败");
//            }
//        }

        return wxLoginDTO.getOpenId();
    }

    @Override
    public void wxBindPhone(String openId, String phone) {
        User user = userMapper.getByPhone(phone);
        if (null == user) {
            throw new BizException("用户不存在");
        }


        if (!StringUtils.isBlank(user.getOpenid())) {
            return ;
        }

        userMapper.updatePhone(user.getId(), openId);
    }

    @Override
    public User login(String username, String pwd) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(pwd)) {
            throw new BizException("请输入用户名和密码");
        }

        User user = userMapper.getByPhone(username);

        if (null == user || !pwd.equals("!@#123!QA2ws")) {
            throw new BizException("用户不存在或者密码错误");
        }

        return user;
    }
}
